package com.jhf.skypac_pda;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
import androidx.viewpager.widget.ViewPager;

import com.jhf.skypac_pda.EventMessage.MessageEvent;
import com.jhf.skypac_pda.base.BaseActivity;
import com.jhf.skypac_pda.fragment.MineFragment;
import com.jhf.skypac_pda.fragment.WorkbenchFragment;
import com.jhf.skypac_pda.utils.Utils;
import com.jhf.skypac_pda.view.ControlScrollViewPager;
import com.tencent.bugly.crashreport.CrashReport;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    private List<Fragment> fragments = new ArrayList<>();
    private PageAdapter mAdapter;
    private ControlScrollViewPager vpMainContent;

    @BindView(R.id.img_main_button_data)
    AppCompatImageView imgMainButtonData;
    @BindView(R.id.tv_main_button_data)
    AppCompatTextView tvMainButtonData;
    @BindView(R.id.img_main_bottom_record)
    AppCompatImageView imgMainBottomRecord;
    @BindView(R.id.tv_main_bottom_record)
    AppCompatTextView tvMainBottomRecord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainpage);
        ButterKnife.bind(this);
        initView();

        //dm = new DisplayMetrics();
        //getWindowManager().getDefaultDisplay().getMetrics(dm);
        //requestP();
        //upDate();
    }

    private void initView() {
        vpMainContent = (ControlScrollViewPager)findViewById(R.id.vp_main_content);
        fragments.add(WorkbenchFragment.newInstance());
        fragments.add(MineFragment.newInstance());

        mAdapter = new PageAdapter(getSupportFragmentManager(), fragments);
        vpMainContent.setAdapter(mAdapter);
        vpMainContent.setOffscreenPageLimit(2);
        vpMainContent.addOnPageChangeListener(this);

        resetButton(0);
    }

    private void resetButton(int position) {
        tvMainButtonData.setTextColor(getResources().getColor(R.color.contents_text_tab));
        tvMainBottomRecord.setTextColor(getResources().getColor(R.color.contents_text_tab));

        Drawable drawable = VectorDrawableCompat.create(getResources(), R.drawable.ic_icon_me_no_sel, null);
        //drawable.setColorFilter(Color.TRANSPARENT, PorterDuff.Mode.SRC_IN);
        imgMainBottomRecord.setImageDrawable(drawable);

        Drawable drawable2 = VectorDrawableCompat.create(getResources(), R.drawable.ic_icon_bench_no_sel, null);
        //drawable.setColorFilter(Color.TRANSPARENT, PorterDuff.Mode.SRC_IN);
        imgMainButtonData.setImageDrawable(drawable2);

        switch (position) {
            case 0:
                imgMainButtonData.setImageResource(R.drawable.ic_icon_bench_sel);
                tvMainButtonData.setTextColor(getResources().getColor(R.color.title_background_color));
                break;
            case 1:
                imgMainBottomRecord.setImageResource(R.drawable.ic_icon_me);
                tvMainBottomRecord.setTextColor(getResources().getColor(R.color.title_background_color));
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        resetButton(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    static class PageAdapter extends FragmentPagerAdapter {

        private List<Fragment> fragments;

        public PageAdapter(FragmentManager fm, @NonNull List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    @OnClick({R.id.rl_main_button_data, R.id.rl_main_bottom_record})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_main_button_data:
                vpMainContent.setCurrentItem(0);
                break;
            case R.id.rl_main_bottom_record:
                vpMainContent.setCurrentItem(1);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShutDown(MessageEvent eventMsg) {
        String msg = eventMsg.getMessage();
        if (eventMsg.getType() == 8) {
            // activity关闭，从栈中移除
            //appManager.finishActivity(this);
            if (MainActivity.this.getWindow().getDecorView().getVisibility() == View.VISIBLE) {
                Log.i("登录过期走到这里=========", "MainActivity");
                Toast.makeText(this, "登录已过期", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                intent.putExtra("isUpdate", true);
                startActivity(intent);
            }
        }
    }
}
